import 'package:FineArt/components/rounded_text.dart';
import 'package:FineArt/core/api/module_api.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:flutter/material.dart';
import 'package:FineArt/constants.dart';

class MHorizontalContainer extends StatefulWidget {
  final double width;
  final double height;
  final double scrollOffset;
  final List<Category> categories;
  final Function onTapItem;

  const MHorizontalContainer({Key key,
    this.height = 25,
    this.width = 0,
    this.scrollOffset = 0.0,
    this.categories,
    this.onTapItem})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState

    return new _MHorizontalContainer();
  }
}

class _MHorizontalContainer extends State<MHorizontalContainer> {
  @override
  Widget build(BuildContext context) {
    ScrollController controller = new ScrollController(initialScrollOffset: widget.scrollOffset);
    Size size = MediaQuery
        .of(context)
        .size;
    double _width = widget.width > 0 ? widget.width : size.width * 0.8;
    return Container(
      height: widget.height,
      margin: EdgeInsets.symmetric(vertical: 0),
      padding: EdgeInsets.symmetric(horizontal: 2, vertical: 0),
      width: _width,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        color: Colors.white,
        // borderRadius: BorderRadius.circular(29),
      ),
      child: ListView.builder(
          controller: controller,
          scrollDirection: Axis.horizontal,
          itemCount: widget.categories.length,
          itemBuilder: (BuildContext context, int index) {
            String label = widget.categories[index].label;
            return RoundedText(
              label: label,
              fontSize: 15,
              selected: widget.categories[index].selected ?? false,
              press: () {
                selectCategory(index, controller.offset);
              },
            );
          }),
    );
  }

  void selectCategory(int index, double offset) {
    setState(() {
      for (Category c in widget.categories) {
        c.selected = false;
      }
      widget.categories[index].selected = true;
    });
    widget.onTapItem(widget.categories[index], offset);
  }
}
